//
//  DatePicker.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/20.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit

class DatePicker: UIViewController {
    var handle:SLBlock?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.closeBtn.isHidden = false
        self.view.backgroundColor = rgba(hex: "#000000", a: 0.7)
        self.titleL.text = "Date selection"
        self.nextButton.setTitle("Confirm", for: .normal)
        self.picker.isHidden = false
    }
    class func showDatePick(page:UIViewController,handle: SLBlock?){
        let picker = DatePicker()
        picker.handle = handle
        picker.modalPresentationStyle = .overCurrentContext
        picker.modalTransitionStyle = .crossDissolve
        page.present(picker, animated: true)
    }
    lazy var backView:UIView = {
        let pt = create_view(bgColor: .white, supView: self.view) { make in
            make.height.equalTo(429)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.bottom.equalTo(0)
        }
        pt.layer.masksToBounds = true
        pt.layer.cornerRadius = 12
        return pt
    }()

    lazy var titleL:UILabel = {
        let pt = create_label(text: "Date selection", textColor: .black,textFont: SLFontConfig.medium18,alignment: .center, supView: self.backView) { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(24)
        }
        pt.numberOfLines = 0
        return pt
    }()
  
    lazy var closeBtn:UIButton = {
        let pt = create_button(image: IMAGE("icon_return"), supView: self.backView, closure: { make in
            make.top.equalTo(25)
            make.left.equalTo(15)
            make.height.width.equalTo(24)
        }, selector: (target: self, action: #selector(buttonClick), event:.touchUpInside))
        pt.layer.cornerRadius = 12

        
        return pt
    }()
    lazy var nextButton:UIButton = {
        let pt = create_button(title: "Confirm", titleColor: .white,titleFont: SLFontConfig.medium16,bgColor: rgba(hex: "#1608EC"), image: nil, supView: self.backView, closure: { make in
            make.bottom.equalTo(-49)
            make.centerX.equalToSuperview()
            make.width.equalTo(281)
            make.height.equalTo(44)
        }, selector: (target: self, action: #selector(buttonClick), event:.touchUpInside))
        pt.layer.cornerRadius = 22
        
        return pt
    }()
    @objc func buttonClick(btn: UIButton){
        if btn == self.closeBtn{
            self.dismiss(animated: true)
        }else{
            self.dismiss(animated: true)
            let time = _date_date_string(date: self.picker.date)
            if let block = self.handle{
                block(time)
            }
        }
    }
    lazy var picker:UIDatePicker = {
        let v = UIDatePicker()
        v.datePickerMode = .date
        v.backgroundColor = .white
        v.preferredDatePickerStyle = .wheels
        v.addTarget(self, action: #selector(datePickerValueChanged), for: .valueChanged)
        v.setValue(UIColor.black, forKey: "textColor")
        self.backView.addSubview(v)
        v.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.bottom.equalTo(self.nextButton.snp.top)
            make.top.equalTo(89)
        }
        return v
    }()
    @objc func datePickerValueChanged(sender: UIDatePicker) {
            let selectedDate = sender.date // 获取选中的日期
            print("Selected date: \(selectedDate)")
            // 你可以在这里处理选中的日期，例如更新UI
        }

}
